﻿using XKJ.OAWeb.Infrastructrue;
using XKJ.OAWeb.Models;
using XKJ.OAWeb.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XKJ.OAWeb.Dto;
using System.Security.Cryptography;

namespace XKJ.OAWeb.Application
{
    public class RoleService : IRoleService
    {
        IBaseRepository<Role> _role;
        IBaseRepository<Limits> _limits;

        public RoleService(IBaseRepository<Role> role, IBaseRepository<Limits> limits)
        {
            _role = role;
            _limits = limits;
        }
        //添加
        public int RoleAdd(Role role)
        {
            role.LId = 0;
            return _role.Add(role);
        }

        //反填
        public Role RoleBackfill(int Id)
        {
            return _role.Backfill(Id);
        }

        //删除
        public bool RoleDeleteId(int Id)
        {
            return _role.DeleteId(Id);
        }
        //角色权限显示
        public PageList<RoleLimitsDto> RoleLimitsShow(int Pageindex, int Pagesize, string? RoleName)
        {
            var list = from a in _role.Show().Where(x => x.RId > 0)
                       join b in _limits.Show() on a.LId equals b.LId
                       select new RoleLimitsDto
                       {
                           RId=a.RId,
                           RoleName=a.RoleName,
                           Describe=a.Describe,
                           LId=a.LId,
                           Details=b.Details
                       };
            if (!string.IsNullOrEmpty(RoleName))
            {
                list = list.Where(x => x.RoleName.Contains(RoleName));
            }

            PageList<RoleLimitsDto> page = new()
            {
                Total=list.Count(),
                Plist=list.Skip((Pageindex-1)*Pagesize).Take(Pagesize).ToList()
            };

            return page;
        }

        //显示
        public PageList<Role> RoleShow(int Pageindex, int Pagesize, string? RoleName)
        {
            var list=_role.Show().Where(x=>x.RId>0).ToList();
            if (RoleName!=null)
            {
                list = list.Where(x => x.RoleName.Contains(RoleName)).ToList();
            }

            PageList<Role> page = new()
            {
                Total=list.Count(),
                Plist = list.Skip((Pageindex - 1) * Pagesize).Take(Pagesize).ToList()
            };
            return page;
        }
        //修改
        public int RoleUptdate(Role role)
        {
            return _role.Uptdate(role);
        }
    }
}
